public class J2411201 {
    public static void main(String[] args) {
        MyList myList = new MyList();
        myList.size();
        myList.createList();
        myList.display();
//        MyList.ListNode newHead = myList.middleDisplay();
//        myList.display(newHead);
//        System.out.println(newHead.val);
//        MyList.ListNode newHead1 = myList.FindKtoTail(4);
//        System.out.println(newHead1.val);
//        myList.allRemove(12);
//        myList.display();
//        myList.clear();
//        myList.display();
        MyList myList1 = new MyList();
        myList1.addLast(12);
        myList1.addLast(23);
        myList1.addLast(34);
        myList1.addLast(45);
        myList1.display();
        MyList myList2 = new MyList();
        myList2.addLast(16);
        myList2.addLast(18);
        myList2.addLast(26);
        myList2.addLast(84);
        myList2.addLast(108);
        myList2.display();
        MyList.ListNode nh = mergeTwoLists(myList1.head,myList2.head);
        myList2.display(nh);
    }
    //将两个有序的链表和成一个有序的链表
    public static MyList.ListNode mergeTwoLists(MyList.ListNode headA, MyList.ListNode headB){
        MyList.ListNode newHead = new MyList.ListNode(-1);
        MyList.ListNode temHead = newHead;

        while (headA != null && headB != null){
            if (headA.val > headB.val){
                temHead.next = headB;
                headB = headB.next;
            }else {
                temHead.next = headA;
                headA = headB.next;
            }
            temHead = temHead.next;
        }
        if (headA != null){
            temHead.next = headA;
        }
        if (headB != null){
            temHead.next = headB;
        }
        return newHead.next;
    }
}
